<?php
namespace App\Wen\Utils;

use App\Models\Meta\WxUserMetaNum;
use App\Models\Posts\WxCollect;
use App\Models\Posts\WxComment;
use App\Models\Posts\WxLike;
use App\Models\Posts\WxPost;
use App\Models\Circle\WxCircle;
use App\Models\WxAuthentication;
use App\Models\WxCommentLike;
use App\Models\WxFeedback;
use App\Models\WxOrder;
use App\Models\Shop\WxShopOrder;
use App\Models\User\WxUser;
use App\Models\User\WxUserFinancial;
use App\Models\User\WxUserFollow;
use App\Models\User\WxUserTemp;
use App\Models\User\WxUserWithdrawal;
use Illuminate\Support\Facades\Cache;

class StatisticUtils{

    public static function totay_payment_count(){
        $order_pay_price = WxOrder::where([
            ['order_state', '=', 1], ['created_at', '>', current_time('day')]
        ])->where('order_serial_number', '!=', '')->whereNotNull('order_serial_number')->sum('order_pay_price');
        $shop_order_payed = WxShopOrder::where([
            ['pay_status', '=', 2],
            ['created_at', '>', current_time('day')]
        ])->where('serial_number', '!=', '')->whereNotNull('serial_number')->sum('payed');
        return $order_pay_price + $shop_order_payed;
    }


    public static function total_user_count(){
//        (int)Cache::remember('total_user', 60, function (){
//            return WxUser::where('user_state', 0)->count();
//        });
        return WxUser::where('user_state', 0)->count();
    }

    public static function total_post_count(){
        return (int)(Cache::remember('total_post_count', 600, function (){
            return WxPost::where('posts_state', 0)->count();
        }));
    }

    public static function today_user_register_count(){
        return WxUser::where('created_at', '>', current_time('day'))->count();
    }

    public static function total_tenant_todo_count($split = false, $cache = false){
        global $__MINI_GLOBAL_TENANT_ID__;
        if($cache){
            return Cache::remember('total:tenant:todo:count',60,  function () use ($split, $__MINI_GLOBAL_TENANT_ID__){
                $todo_circle = WxCircle::where('circle_state', 0)->where('tenant_id', $__MINI_GLOBAL_TENANT_ID__)->count();
                $todo_post = WxPost::where('is_examine', 0)->where('tenant_id', $__MINI_GLOBAL_TENANT_ID__)->count();
                $todo_comment = WxComment::where('comment_state', 0)->whereHas('post', function ($query) use ($__MINI_GLOBAL_TENANT_ID__){
                    $query->where('tenant_id', $__MINI_GLOBAL_TENANT_ID__);
                })->count();

                if($split){
                    return [$todo_post, $todo_comment, $todo_circle];
                }else{
                    return $todo_post + $todo_comment + $todo_circle;
                }
            });
        }else{
            $todo_circle = WxCircle::where('circle_state', 0)->where('tenant_id', $__MINI_GLOBAL_TENANT_ID__)->count();
            $todo_post = WxPost::where('is_examine', 0)->where('tenant_id', $__MINI_GLOBAL_TENANT_ID__)->count();
            $todo_comment = WxComment::where('comment_state', 0)->whereHas('post', function ($query) use ($__MINI_GLOBAL_TENANT_ID__){
                $query->where('tenant_id', $__MINI_GLOBAL_TENANT_ID__);
            })->count();
            if($split){
                return [$todo_post, $todo_comment, $todo_circle];
            }else{
                return $todo_post + $todo_comment + $todo_circle;
            }
        }
    }

    public static function total_todo_count($split = false, $cache = false){
        if($cache){
            return Cache::remember('total:todo:count',60,  function () use ($split){
                $todo_post = WxPost::where('is_examine', 0)->count();
                $todo_comment = WxComment::where('comment_state', 0)->count();
                $todo_circle = WxCircle::where('circle_state', 0)->count();
                $todo_withdrawal = WxUserWithdrawal::whereIn('state', [0, 2])->count();
                $todo_usertemp = WxUserTemp::where('user_state', 0)->count();
                $todo_feedback = WxFeedback::where('feedback_state', 0)->count();
                $todo_auth = WxAuthentication::where('authentication_state', 0)->count();
                if($split){
                    return [$todo_post, $todo_comment, $todo_circle, $todo_withdrawal, $todo_usertemp, $todo_feedback, $todo_auth];
                }else{
                    return $todo_post + $todo_comment + $todo_circle + $todo_withdrawal + $todo_usertemp + $todo_feedback + $todo_auth;
                }
            });
        }else{
            $todo_post = WxPost::where('is_examine', 0)->count();
            $todo_comment = WxComment::where('comment_state', 0)->count();
            $todo_circle = WxCircle::where('circle_state', 0)->count();
            $todo_withdrawal = WxUserWithdrawal::whereIn('state', [0, 2])->count();
            $todo_usertemp = WxUserTemp::where('user_state', 0)->count();
            $todo_feedback = WxFeedback::where('feedback_state', 0)->count();
            $todo_auth = WxAuthentication::where('authentication_state', 0)->count();
            if($split){
                return [$todo_post, $todo_comment, $todo_circle, $todo_withdrawal, $todo_usertemp, $todo_feedback, $todo_auth];
            }else{
                return $todo_post + $todo_comment + $todo_circle + $todo_withdrawal + $todo_usertemp + $todo_feedback + $todo_auth;
            }
        }
    }

    public static function user_follow_count($uid){
        return (int)Cache::remember('user:follow:count:'.$uid, 3600, function () use($uid){
            return WxUserFollow::where('user_id',$uid)->count();
        });
    }

    public static function user_fans_count($uid){
        return (int)Cache::remember('user:fans:count:'.$uid, 3600, function () use ($uid){
            return WxUserFollow::where('user_follow_id',$uid)->count();
        });
    }

    public static function user_beliked_count($uid){
        return (int)Cache::remember('user:beliked:count:'.$uid, 3600, function () use ($uid){
            $count1 = WxLike::where('posts_user_id',$uid)->count();
            $count2 = WxCommentLike::where('comment_user_id',$uid)->count();
            return $count1+$count2;
        });
    }

    public static function user_post_count($uid){
        return (int)Cache::remember('user:post:count:'.$uid, 3600, function () use ($uid){
            return WxPost::where([['user_id','=', $uid], ['posts_state', '=', 0]])->count();
        });
    }

    public static function user_becollected_count($uid){
        return (int)Cache::remember('user:becollected:count:'.$uid, 3600, function () use ($uid){
            return WxCollect::where('posts_user_id', $uid)->count();
        });
    }

    public static function post_becollected_count($post_id){
        return (int)Cache::remember('post:becollected:count:'.$post_id, 3600, function () use ($post_id){
            return WxCollect::where('posts_id', $post_id)->count();
        });
    }

    public static function get_coins_total(){
        return WxUserMetaNum::where('meta_key', 'coins')->sum('meta_value');
    }

    public static function get_blance_total(){
        return WxUserFinancial::sum('balance');
    }


}
